package com.happyass.controller;

import com.happyass.dao.EvaluateDao;
import com.happyass.dao.UserDao;
import com.happyass.pojo.Evaluate;
import com.happyass.pojo.Staff;
import com.happyass.pojo.User;
import com.happyass.pojo.page.PageInfo;
import com.happyass.service.EvaluateService;
import com.happyass.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/evaluate")
public class EvaluateController {
      //注入
    @Autowired
    private EvaluateService evaluateService;

    //访问评价页面
    @RequestMapping("/list")
    public String list(Model model) {
        List<Evaluate> evaluateList = evaluateService.selectAll();
        model.addAttribute("list", evaluateList);
        return "Table/evaluatePJTable";
    }
//-----------------------------------------------------------------
    //站点评价
    @RequestMapping("/pj")
    public String star(Model model, PageInfo pageInfo) {


//        ------------------------
        Page<Evaluate> evaluatePage  = evaluateService.selectAllevaluate(pageInfo.getPage(),pageInfo.getRow());
        //当前页
        model.addAttribute("curPage",evaluatePage.getNumber()+1);
        //下一页
        if ((evaluatePage.getNumber()+2)> evaluatePage.getTotalPages()){
            model.addAttribute("nextPage",(evaluatePage.getNumber()+1));}
        else {
            model.addAttribute("nextPage",(evaluatePage.getNumber())+2);
        }
        //上一页
        if (evaluatePage.getNumber()<=0) {
            model.addAttribute("prePage", evaluatePage.getNumber()+1);
        }else{
            model.addAttribute("prePage", evaluatePage.getNumber());
        }
        //总页数
        model.addAttribute("totalPages",evaluatePage.getTotalPages());
        //总条数
        model.addAttribute("totalElements",evaluatePage.getTotalElements());
        model.addAttribute("list",evaluatePage.getContent());
        List<Evaluate> evaluateList = evaluateService.selectByStaus();
//        model.addAttribute("list", evaluateList);
        //站点查询平均分
        double score = evaluateService.averageStar();
        model.addAttribute("score",score);
        return "evaluate/evaluateBack";}


    //站点评价
    @RequestMapping("/viewPj")
    public String viewPj(Model model, PageInfo pageInfo) {


//        ------------------------
        Page<Evaluate> evaluatePage  = evaluateService.selectAllevaluate(pageInfo.getPage(),pageInfo.getRow());
        //当前页
        model.addAttribute("curPage",evaluatePage.getNumber()+1);
        //下一页
        if ((evaluatePage.getNumber()+2)> evaluatePage.getTotalPages()){
            model.addAttribute("nextPage",(evaluatePage.getNumber()+1));}
        else {
            model.addAttribute("nextPage",(evaluatePage.getNumber())+2);
        }
        //上一页
        if (evaluatePage.getNumber()<=0) {
            model.addAttribute("prePage", evaluatePage.getNumber()+1);
        }else{
            model.addAttribute("prePage", evaluatePage.getNumber());
        }
        //总页数
        model.addAttribute("totalPages",evaluatePage.getTotalPages());
        //总条数
        model.addAttribute("totalElements",evaluatePage.getTotalElements());
        model.addAttribute("list",evaluatePage.getContent());
        List<Evaluate> evaluateList = evaluateService.selectByStaus();
//        model.addAttribute("list", evaluateList);
        //站点查询平均分
        double score = evaluateService.averageStar();
        model.addAttribute("score",score);
        return "evaluate/evaluateView";}


    //-----------------------------------------------------------------------
     //修改显示状态
     @RequestMapping("/show")
     @ResponseBody
     public Boolean show(Integer evaluateId,Integer evaluateStatus) {
         return evaluateService.editStatus(evaluateId,evaluateStatus);}

    @Autowired
    EvaluateDao evaluateDao;
    //搜索
    @RequestMapping("/search")
    public String search(Integer evaluateStar,Model model, PageInfo pageInfo) {
        Page<Evaluate> evaluatePage  = evaluateService.selectAllevaluate(pageInfo.getPage(),pageInfo.getRow());
        //当前页
        model.addAttribute("curPage",evaluatePage.getNumber()+1);
        //下一页
        if ((evaluatePage.getNumber()+2)> evaluatePage.getTotalPages()){
            model.addAttribute("nextPage",(evaluatePage.getNumber()+1));}
        else {
            model.addAttribute("nextPage",(evaluatePage.getNumber())+2);
        }
        //上一页
        if (evaluatePage.getNumber()<=0) {
            model.addAttribute("prePage", evaluatePage.getNumber()+1);
        }else{
            model.addAttribute("prePage", evaluatePage.getNumber());
        }
        //总页数
        model.addAttribute("totalPages",evaluatePage.getTotalPages());
        //总条数
        model.addAttribute("totalElements",evaluatePage.getTotalElements());
        Evaluate evaluate = new Evaluate();
        //页面传来的staffId
        evaluate.setEvaluateStar(evaluateStar);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("evaluateStar", ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询匹配开头，即{username}%//全部模糊查询，即%{address}%

        Example<Evaluate> example = Example.of(evaluate, matcher);
        Pageable pageable = PageRequest.of(pageInfo.getPage() - 1, pageInfo.getRow());
        //通过id找到用户
        Page<Evaluate> list = evaluateDao.findAll(example,pageable);
        model.addAttribute("list",list);
        model.addAttribute("evaluateStar",evaluateStar);
        return "Table/evaluatePJTableSearch";
    }

    //分页
    @RequestMapping("/allPageList")
    public String allPageList(Model model, PageInfo pageInfo){
        Page<Evaluate> evaluatePage  = evaluateService.selectAllevaluate(pageInfo.getPage(),pageInfo.getRow());
        //当前页
        model.addAttribute("curPage",evaluatePage.getNumber()+1);
        //下一页
        if ((evaluatePage.getNumber()+2)> evaluatePage.getTotalPages()){
            model.addAttribute("nextPage",(evaluatePage.getNumber()+1));}
        else {
            model.addAttribute("nextPage",(evaluatePage.getNumber())+2);
        }
        //上一页
        if (evaluatePage.getNumber()<=0) {
            model.addAttribute("prePage", evaluatePage.getNumber()+1);
        }else{
            model.addAttribute("prePage", evaluatePage.getNumber());
        }
        //总页数
        model.addAttribute("totalPages",evaluatePage.getTotalPages());
        //总条数
        model.addAttribute("totalElements",evaluatePage.getTotalElements());
        //userInfoPage.getContent() 当期页集合
        model.addAttribute("list",evaluatePage.getContent());
        return "Table/evaluatePJTable";
    }
    //访问增加评价
    @RequestMapping("/add")
    public String Add() {
        return "evaluate/evaluateAdd";
    }
    @ResponseBody
    @RequestMapping("/submitAdd")
    public HashMap<String, Object> insertEvaluate( String evaluateTime,String evaluateContent,Integer evaluateStar ) throws ParseException {
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        evaluateTime = dateTime.format(formatter);
        Subject subject = SecurityUtils.getSubject();
        Staff staffTemp = (Staff) subject.getPrincipal();
        String staffUsername = staffTemp.getStaffUsername();

        return evaluateService.insertEvaluate(staffUsername,evaluateTime,evaluateContent,evaluateStar);
    }
}